package pCloud.stream;

import java.util.Vector;

/*
 * The Aggregate Box class
 * Each aggregate box has: window type, window size, window advance step
 * a list of aggregate attributes and their corresponding aggregate function
 * 
 */

public class AggregateBox {

	public String windowtype;
	public int windowsize;
	public int windowadvancestep;
	public Vector<String> aggregatefunction;
	public Vector<String> aggregateattribute;
	
	public String mergeMsg;
	
	public AggregateBox() {
		this.mergeMsg = "";
		this.aggregateattribute = new Vector<String>();
		this.aggregatefunction= new Vector<String>();
	}
	
	public AggregateBox(String type, int size, int step){
		
		this.mergeMsg = "";
		this.windowtype = type;
		this.windowsize = size;
		this.windowadvancestep = step;
		this.aggregateattribute = new Vector<String>();
		this.aggregatefunction= new Vector<String>();		
	}
	
	public void print() {
		
		System.out.println("Window Type : "+ this.windowtype);
		System.out.println("Window Size : "+ this.windowsize);
		System.out.println("Window Advance Step : "+ this.windowadvancestep);

		for (int i=0; i< this.aggregateattribute.size(); i++){
			System.out.println(this.aggregatefunction.elementAt(i)+"("+this.aggregateattribute.elementAt(i) + ")");
		}		
	}
	
	/*
	public String toXACML() {
		
		String xacml="";
		
		//window specification
		
		xacml += "<Obligation ObligationId=\"" + PCloudAttributeAndType.OBLIGATION_STREAM_WINDOW + "\" FulfillOn=\"Permit\">" + Global.eol;    
	    xacml += "<AttributeAssignment AttributeId=\"" + PCloudAttributeAndType.OBLIGATION_STREAM_WINDOW_STEP + "\" DataType=\"" + PCloudAttributeAndType.TYPE_INTEGER+"\">";  
		xacml += this.windowadvancestep;
	    xacml += "</AttributeAssignment>" + Global.eol;
	    
	    xacml += "<AttributeAssignment AttributeId=\"" + PCloudAttributeAndType.OBLIGATION_STREAM_WINDOW_SIZE + "\" DataType=\"" + PCloudAttributeAndType.TYPE_INTEGER+"\">";  
		xacml += this.windowsize;
	    xacml += "</AttributeAssignment>" + Global.eol;

	    xacml += "<AttributeAssignment AttributeId=\"" + PCloudAttributeAndType.OBLIGATION_STREAM_WINDOW_TYPE + "\" DataType=\"" + PCloudAttributeAndType.TYPE_STRING+"\">";  
		xacml += this.windowtype;
	    xacml += "</AttributeAssignment>" + Global.eol;
	    
	    xacml += "</Obligation>" + Global.eol;

	    // attribute and function specification;
	    
	    for(int i=0; i<this.aggregateattribute.size();i++){
	    		
	    		xacml += "<Obligation ObligationId=\"" + PCloudAttributeAndType.OBLIGATION_STREAM_ATTRIBUTE_AGGREGATION + "\" FulfillOn=\"Permit\">" + Global.eol;    
		    xacml += "<AttributeAssignment AttributeId=\"" + PCloudAttributeAndType.OBLIGATION_STREAM_WINDOW_FUNCTION+ "\" DataType=\"" + PCloudAttributeAndType.TYPE_STRING+"\">";  
			xacml += this.aggregatefunction.elementAt(i);
		    xacml += "</AttributeAssignment>" + Global.eol;
		    
		    xacml += "<AttributeAssignment AttributeId=\"" + PCloudAttributeAndType.OBLIGATION_STREAM_WINDOW_ATTRIBUTE + "\" DataType=\"" + PCloudAttributeAndType.TYPE_STRING+"\">";  
			xacml += this.aggregateattribute.elementAt(i);
		    xacml += "</AttributeAssignment>" + Global.eol;
		    
		    xacml += "</Obligation>" + Global.eol;

	    }
	    	    
		
		return xacml;
		
	}
	*/
	
	/* 
	 * Create the corresponding SSQL statement of the aggregate box
	 */
	public String createSSQL(String input, String output){
		
		String ssql="";
		String type="";
		if (this.windowtype.equalsIgnoreCase("tuple")){
			type="TUPLES";
		}
		else if (this.windowtype.equalsIgnoreCase("time")){
			type="TIME";
		}
		else {}
		String windowname = "_" + this.windowsize+this.windowtype;
		ssql += "CREATE WINDOW " + windowname + "( SIZE " + this.windowsize + " ADVANCE " + this.windowadvancestep + " " + type + ")"; 
		ssql += ";" + Global.eol;
		
		ssql +="SELECT " + Global.eol;
		
		for(int i=0; i<this.aggregateattribute.size()-1; i++){
			
			ssql += this.aggregatefunction.elementAt(i) + "(" + this.aggregateattribute.elementAt(i) + ")";
			ssql += " AS " + this.aggregatefunction.elementAt(i) + this.aggregateattribute.elementAt(i) + "," + Global.eol;
		}
		
		ssql += this.aggregatefunction.elementAt(this.aggregatefunction.size()-1) + "(" + this.aggregateattribute.elementAt(this.aggregateattribute.size()-1) + ")";
		ssql += " AS " + this.aggregatefunction.elementAt(this.aggregatefunction.size()-1) + this.aggregateattribute.elementAt(this.aggregatefunction.size()-1)+ Global.eol ;

		ssql += "FROM " + input + "[" + windowname + "]" + Global.eol + "INTO " + output + ";";		
		return ssql;		
	}
}
